<?php

declare (strict_types=1);

namespace app\matter\controller;

use app\matter\BaseController;
use app\admin\validate\DepartmentCheck;
use app\matter\model\AreaModel;
use app\matter\model\GeneralMaterialModel;
use app\matter\model\MaterialModel;
use app\matter\model\MatterCaseModel;
use app\matter\model\MatterReviewPointsModel;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\View;

//情形管理
class MatterCase extends BaseController
{
    public function index()
    {
        $param = get_params();
        if (request()->isAjax()) {
            $list = MatterCaseModel::with('material')->where('matter_id',$param['matter_id'])->select()->toArray();
            foreach ($list as $k=>$v){
                $list[$k]['material_list'] = '';
                foreach ($v['material'] as $value){
                    $list[$k]['material_list'] .= $value['name'].';';
                }
            }
            return to_assign(0, '', $list);
        } else {

            View::assign('matter_id', $param['matter_id']);
            return view();
        }
    }

    //添加
    public function add()
    {
        $param = get_params();
        if (request()->isAjax()) {
            if ($param['id'] > 0) {
                $department_array = get_matter_case_son($param['id']);
                if (in_array($param['pid'], $department_array)) {
                    return to_assign(1, '上级情形不能是该情形本身或其下属情形');
                } else {
                    MatterCaseModel::update($param);
                    add_log('edit', $param['id'], $param);
                    $case_id = $param['id'];
                }
            } else {
                $param['uid'] = $this->uid;
                unset($param['id']);
                $model = MatterCaseModel::create($param);
                add_log('add', $model->id, $param);
                $case_id = $model->id;

            }
            //审查要点
            foreach ($param['points']['id'] as $key => $value) {
                if (empty($param['points']['content'][$key])) {
                    continue;
                }
                $data = [];
                $data['id'] = $value;
                $data['content'] = $param['points']['content'][$key];
                $data['matter_id'] = $param['matter_id'];
                $data['case_id'] = $case_id;
                $data['uid'] = $this->uid;
                if ($data['id'] > 0) {
                    //修改
                    $Model = MatterReviewPointsModel::find($data['id']);
                    $Model->save($data);
                } else {
                    //新增
                    $Model = new MatterReviewPointsModel();
                    $Model->save($data);
                }
            }
            //材料
            if (!empty($param['material'])) {
                foreach ($param['material']['id'] as $key => $value) {
                    if (empty($param['material']['name'][$key])) {
                        continue;
                    }
                    $data = [];

                    $data['id'] = $value;
                    $data['name'] = $param['material']['name'][$key];
                    if(empty($param['material']['shape'][$key])){
                        return to_assign(1, '材料['.$data['name'].']：请选择材料形式');
                    }
                    $data['shape'] = $param['material']['shape'][$key];
                    $data['type'] = $param['material']['type'][$key];
                    $data['number'] = $param['material']['number'][$key];
                    if(empty($param['material']['necessity'][$key])){
                        return to_assign(1, '材料['.$data['name'].']：请选择必要性');
                    }
                    $data['necessity'] = $param['material']['necessity'][$key];
                    if(empty($param['material']['source'][$key])){
                        return to_assign(1, '材料['.$data['name'].']：请选择来源');
                    }
                    $data['source'] = $param['material']['source'][$key];
                    $data['source_info'] = $param['material']['source_info'][$key];
                    $data['sample_table'] = $param['material']['sample_table'][$key];
                    $data['empty_table'] = $param['material']['empty_table'][$key];
                    $data['template'] = $param['material']['template'][$key];
                    $data['exempt'] = $param['material']['exempt'][$key];
                    $data['notice'] = $param['material']['notice'][$key];
                    $data['standard'] = $param['material']['standard'][$key];
                    $data['case_id'] = $case_id;
                    $data['uid'] = $this->uid;

                    if ($data['id'] > 0) {
                        //修改
                        $Model = MaterialModel::find($data['id']);
                        $Model->save($data);
                    } else {
                        //新增
                        $Model = new MaterialModel();
                        $Model->save($data);
                    }
                }
            }
            return to_assign();
        } else {
            $id = isset($param['id']) ? $param['id'] : 0;
            $pid = isset($param['pid']) ? $param['pid'] : 0;
            $case_list = set_recursion(get_case_list($param['matter_id']));
            View::assign('case_list', $case_list);
            if ($id > 0) {
                $detail = MatterCaseModel::with(['material','points'])->find($id)->toArray();
                if (!empty($detail['material'])) {
                    //材料
                    foreach ($detail['material'] as $k => $v) {
                        if (!empty($v['sample_table'])) {
                            $fileArray = Db::name('File')->where('id', 'in', $v['sample_table'])->select();
                            $detail['material'][$k]['sample_tableArray'] = $fileArray;
                        }
                        if (!empty($v['empty_table'])) {
                            $fileArray = Db::name('File')->where('id', 'in', $v['empty_table'])->select();
                            $detail['material'][$k]['empty_tableArray'] = $fileArray;
                        }
                        if (!empty($v['template'])) {
                            $fileArray = Db::name('File')->where('id', 'in', $v['template'])->select();
                            $detail['material'][$k]['templateArray'] = $fileArray;
                        }
                    }

                }

                View::assign('detail', $detail);
            }
            View::assign('pid', $pid);
            View::assign('id', $id);
            View::assign('matter_id', $param['matter_id']);
            //通用材料
            $general_material = GeneralMaterialModel::select()->toArray();
            View::assign('general_material', $general_material);
            return view();
        }
    }

    //删除
    public function delete()
    {
        $id = get_params("id");
        $count = MatterCaseModel::where('pid', $id)->count();
        if ($count > 0) {
            return to_assign(1, "该情形下还有子情形，无法删除");
        }
        $Model = MatterCaseModel::find($id);
        $Model->delete();
        add_log('delete', $id);
        return to_assign(0, "删除成功");
    }

    //材料 删除
    public function material_delete()
    {
        $param = get_params();
        $Model = MaterialModel::find($param['id']);
        $Model->delete();
        return to_assign();
    }
}
